﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using TIMETABLEASY.TokenAuthentification;
using TIMETABLEASY.EasyCalendar.Model;

namespace TIMETABLEASY.EasyCalendar.Controler
{
    public static class DateEventFilter
    {
        public static IEnumerable<EventCal> ByDate(this IEnumerable<EventCal> events, DateTime date)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();
            ObservableCollection<EventCal> vect = new ObservableCollection<EventCal>();
            event_management event1 = new event_management();

            //Load the personal calendar in the vector
            if (token.Is_perso_selected)
            {
                ArrayList new_event = event1.give_all_perso_event(date, token.User_id, token.User_level);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }

            //load the University calendar
            if (token.TypeOfView == "University")
            {
                ArrayList new_event = event1.give_all_university_event(date);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }
            //Load the campus university
            else if (token.TypeOfView == "Campus")
            {
                ArrayList new_event = event1.give_all_campus_event(date, token.ValueOfView);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }
            //load the cursus
            else if (token.TypeOfView == "Cursus")
            {
                ArrayList new_event = event1.give_all_cursus_event(date, token.ValueOfView);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }
            //load the class event
            else if (token.TypeOfView == "Class")
            {
                ArrayList new_event = event1.give_all_class_event(date, token.ValueOfView);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }
            //load event of a period of a campus
            else if (token.TypeOfView == "Period")
            {
                ArrayList new_event = event1.give_all_period_event(date, token.ValueOfView, token.ValueOfView2nd);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }
            //load a user events
            else if (token.TypeOfView == "User")
            {
                ArrayList new_event = event1.give_all_user_event(date, token.ValueOfView, token.ValueOfView2nd);
                IEnumerator personListEnumerator = new_event.GetEnumerator();

                while (personListEnumerator.MoveNext())
                {
                    vect.Add(create_event_cal((Event)personListEnumerator.Current));
                }
            }


            return (IEnumerable<EventCal>)vect; 
        }

        //Create a EventCal object
        private static EventCal create_event_cal(Event evnt)
        {
            EventCal ev = new EventCal();

            ev.Subject = evnt.name;
            ev.Location = evnt.campus;

            ev.Class_name = evnt.classe;

            if (evnt.priority == 1)
            {
                ev.Priority_color = "Red";
                ev.Priority_thinkness = 3;
            }
            else
            {
                ev.Priority_color = "Green";
                ev.Priority_thinkness = 1;
            }

            if (evnt.type == "E-Learning")
            {
                ev.TypCalendar = EventCal.ELearningCalendar;
            }
            else if (evnt.type == "Personal")
            {
                ev.TypCalendar = EventCal.PersoCalendar;
            }
            else if (evnt.type == "Face-to-Face")
            {
                ev.TypCalendar = EventCal.PresentCalendar;
            }

            ev.Intervenant = evnt.person_name;

            ev.StartTime = evnt.t_begin;
            ev.EndTime = evnt.t_end;

            ev.Room = evnt.room;
            ev.Period = evnt.period;
            
            return ev;
        }
    }
    
}

